package com.spring.BusinessComponents;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.map.ListOrderedMap;
import org.apache.log4j.Logger;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;

import com.general.genericUtils.ExceptionTrace;
import com.serviceBeans.AlliBilliForum;
import com.serviceBeans.PlaceMyAd;

public class DoTransaction {
	private static Logger log = Logger.getLogger(DoTransaction.class);
	JdbcTemplate jdbcTemplate;
	NamedParameterJdbcTemplate namedParameterJdbcTemplate;
	public JdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	}
	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}
	public NamedParameterJdbcTemplate getNamedParameterJdbcTemplate() {
		return namedParameterJdbcTemplate;
	}
	public void setNamedParameterJdbcTemplate(
			NamedParameterJdbcTemplate namedParameterJdbcTemplate) {
		this.namedParameterJdbcTemplate = namedParameterJdbcTemplate;
	}
	public int countForumData() throws Exception
	{
		String query = com.spring.SQLQueries.COUNT_FORUM_DATA;
		return (getJdbcTemplate().queryForInt(query));
	}
	public int countPlaceMyAdsData() throws Exception
	{
		String query = com.spring.SQLQueries.COUNT_PLACE_MY_DATA;
		return (getJdbcTemplate().queryForInt(query));
	}
	public List<AlliBilliForum> loadForumData(final int startOffset) throws Exception
	{
		String query = com.spring.SQLQueries.LIST_FORUM_DATA;
		List<ListOrderedMap> list =  (getJdbcTemplate().queryForList(query));

		List<AlliBilliForum> forumList = new ArrayList<AlliBilliForum>();
		Iterator<ListOrderedMap> it = list.listIterator();
		while(it.hasNext())
		{
			Map map = (ListOrderedMap)it.next();
			
			long idAlliBilliForum = (Long)map.get("idAlliBilliForum");
			String name =(String)map.get("name");
			String content =(String)map.get("content");
			Timestamp postedDate =(Timestamp)map.get("postedDate");
			AlliBilliForum forum = new AlliBilliForum();
			forum.setContent(content);;
			forum.setIdAlliBilliForum(idAlliBilliForum);
			forum.setName(name);
			forum.setPostedDate(postedDate);
			forumList.add(forum);
			
		}
		
		return forumList;
		
	}
	public List<PlaceMyAd> loadPlaceMyAdsData(final int startOffset) throws Exception
	{
		String query = com.spring.SQLQueries.LIST_PLACE_MY_DATA;
		List<ListOrderedMap> list =  (getJdbcTemplate().queryForList(query));
		
		List<PlaceMyAd> myAdsList = new ArrayList<PlaceMyAd>();
		Iterator<ListOrderedMap> it = list.listIterator();
		while(it.hasNext())
		{
			Map map = (ListOrderedMap)it.next();
			
			long idplaceMyAd= (Long)map.get("idplaceMyAd");
			Timestamp placedDate = (Timestamp)map.get("placedDate");
			String name = (String)map.get("name");
			String country = (String)map.get("country");
			String adTitle = (String)map.get("adTitle");
			String gender= (String)map.get("gender");
			String contactNumber=(String)map.get("contactNumber");
			String contactEmail= (String)map.get("contactEmail");
			String description= (String)map.get("description");
			String city = (String)map.get("city");
			String state = (String)map.get("state");
			
			PlaceMyAd myAd = new PlaceMyAd();
			myAd.setAdTitle(adTitle);
			myAd.setCity(city);
			myAd.setContactEmail(contactEmail);
			myAd.setContactNumber(contactNumber);
			myAd.setCountry(country);
			myAd.setDescription(description);
			myAd.setGender(gender);
			myAd.setIdplaceMyAd(idplaceMyAd);
			myAd.setName(name);
			myAd.setPlacedDate(placedDate);
			myAd.setState(state);
			
			myAdsList.add(myAd);
			
		}
		return myAdsList;
		
	}
	public void saveForumData(AlliBilliForum forum) throws Exception
	{
		log.info("Start Doing Transaction - AlliBilliForum");
		KeyHolder keyHolder = new GeneratedKeyHolder();
		SqlParameterSource parametersSource = new BeanPropertySqlParameterSource(forum);
		System.out.println(parametersSource);
		getNamedParameterJdbcTemplate().update(com.spring.SQLQueries.SAVE_FORUM_DATA,parametersSource, keyHolder);
	}
	public void saveException(ExceptionTrace exceptionTrace)
	{
		log.info("Start Doing Transaction - ExceptionTrace");
		KeyHolder keyHolder = new GeneratedKeyHolder();
		SqlParameterSource parametersSource = new BeanPropertySqlParameterSource(
				exceptionTrace);
		System.out.println(parametersSource);
		getNamedParameterJdbcTemplate().update(com.spring.SQLQueries.SAVE_EXCEPTION_TRACE,
				parametersSource, keyHolder);
		
	}
	public void savePlacemyAdData(PlaceMyAd placeMyAd) throws Exception
	{
		log.info("Start Doing Transaction - PlaceMyAd");
		KeyHolder keyHolder = new GeneratedKeyHolder();
		SqlParameterSource parametersSource = new BeanPropertySqlParameterSource(placeMyAd);
		System.out.println(parametersSource);
		getNamedParameterJdbcTemplate().update(com.spring.SQLQueries.SAVE_PLACE_MY_DATA,parametersSource, keyHolder);
	}
}


